//
//  FeaturedViewController.swift
//  AntProj
//
//  Created by 陈宇 on 2017/5/2.
//  Copyright © 2017年 🐜. All rights reserved.
//

import UIKit
import ObjectMapper
import PullToRefresh

class FeaturedViewController: ListingController {
    
    override func configureLeftItem() {
        addLeftBarButtonWithImage(#imageLiteral(resourceName: "menu"))
    }
    
    override func configureUI() {
        super.configureUI()
        
        title = "MY FEATURED"
    }
    
    override func refreshData(_ position: Position = .top) {
        api.request(.FeaturedHouses).unWrapper(error: { code in
            self.tableView.endRefreshing(at: position)
        }).mapArray(type: House.self).subscribe(onNext: { (houses) in
            self.tableView.endRefreshing(at: position)
            self.dataSource = houses.map { [$0] }
            self.tableView.reloadData()
        }, onError: { _ in
            self.tableView.endRefreshing(at: position)
        }).addDisposableTo(disposeBag)
    }
    
    override func refreshing(_ position: Position) {
        switch position {
        case .top:
            dataSource.removeAll()
            refreshData()
        case .bottom:
            refreshData(.bottom)
        }
    }

}
